package stack;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

/**
 * @author : Rookie
 * @description : 逆波兰表达式求值
 * @date : 2023/11/5
 */
public class InPolishExpressionEvaluate {

    private static List<String> opers = new ArrayList<String>() {{
       add("+");
       add("-");
       add("*");
       add("/");
    }};

    public static void main(String[] args) {
        String[] input = {"10", "6", "9", "3", "+", "-11", "*", "/", "*", "17", "+", "5", "+"};
        Stack<Integer> stack = new Stack<>();
        for (String str : input) {
            if (opers.contains(str)) {
                Integer a = stack.pop();
                Integer b = stack.pop();
                int result = calculate(a, b, str);
                stack.push(result);
            } else {
                stack.push(Integer.parseInt(str));
            }
        }
        System.out.println(stack.peek());
    }

    public static int calculate(int a,int b,String oper) {
        int result = 0;
        switch (oper) {
            case "+": result = a+b;
                        break;
            case "-": result = b-a;
                        break;
            case "*": result = a*b;
                        break;
            case "/": result = b/a;
                         break;
            default: break;
        }
        return result;
    }
}
